package cn.kgc.mushroomdatabaseprovider.service.serviceimpl;

import cn.kgc.mushroomcommon.pojo.*;
import cn.kgc.mushroomcommon.vo.ShopVO;
import cn.kgc.mushroomdatabaseprovider.mapper.CollMapper;
import cn.kgc.mushroomdatabaseprovider.mapper.CommodityMapper;
import cn.kgc.mushroomdatabaseprovider.mapper.ShopMapper;
import cn.kgc.mushroomdatabaseprovider.mapper.ShopcommMapper;
import cn.kgc.mushroomdatabaseprovider.service.ShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class ShopServiceImpl implements ShopService {

    @Resource
    ShopMapper shopMapper;

    @Resource
    CollMapper collMapper;

    @Override
    public List<Shop> selectAll() {
         List<Shop> shops = shopMapper.selectByExample(null);
        return shops;
    }

    @Override
    public Shop selectByid(Integer id) {
        Shop shop = shopMapper.selectByPrimaryKey(id);
        return shop;

    }

    @Override
    public Shop selectShopComId(Integer id) {
        return shopMapper.selectShopComId(id);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public int insertshopanduser(Integer shopcollid, Integer userid) {
         int insertshopanduser = shopMapper.insertshopanduser(shopcollid, userid);
        return insertshopanduser;
    }

    @Override
    public List<Coll> selectCollShop(Integer userid) {
         List<Coll> colls = collMapper.selectCollShop(userid);
        return colls;
    }

    @Resource
    ShopcommMapper shopcommMapper;
    @Resource
    CommodityMapper commodityMapper;

    @Override
    public List<ShopVO> selectShopByName(String shopName) {
        List<ShopVO> shopVOS = new ArrayList<>();
        ShopExample shopExample = new ShopExample();
        ShopExample.Criteria shopExampleCriteria = shopExample.createCriteria();
        shopExampleCriteria.andShopnameEqualTo(shopName);
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        for(Shop shop : shops){
            ShopVO shopVO = new ShopVO();
            BeanUtils.copyProperties(shop,shopVO);
            Integer id = shop.getId();
            ShopcommExample shopcommExample = new ShopcommExample();
            ShopcommExample.Criteria shopcommExampleCriteria = shopcommExample.createCriteria();
            shopcommExampleCriteria.andShopidEqualTo(id);
            List<Shopcomm> shopcomms = shopcommMapper.selectByExample(shopcommExample);
            for(Shopcomm shopcomm : shopcomms){
                Integer commid = shopcomm.getCommid();
                Commodity commodity = commodityMapper.selectByPrimaryKey(commid);
                shopVO.getCommodities().add(commodity);
            }
            shopVOS.add(shopVO);
        }
        return shopVOS;
    }
}
